package nc.hw;

public class HJ34 {

    private static String sort(String word) {
        int[] nums = new int[10];
        int[] lower = new int[26];
        int[] upper = new int[26];
        int numCount = 0;
        int upperCount = 0;
        char[] chars = word.toCharArray();
        for (char aChar : chars) {
            if (aChar >= '0' && aChar <= '9') {
                nums[aChar - '0'] ++;
                numCount ++;
            } else if (aChar >= 'a' && aChar <= 'z') {
                lower[aChar - 'a'] ++;
            } else if (aChar >= 'A' && aChar <= 'Z') {
                upper[aChar - 'A'] ++;
                upperCount ++;
            }
        }
        for (int i = 1; i < nums.length; i++) {
            nums[i] = nums[i] + nums[i - 1];
        }
        for (int i = 1; i < 26; i++) {
            upper[i] = upper[i] + upper[i-1];
            lower[i] = lower[i] + lower[i-1];
        }
        StringBuilder sb = new StringBuilder();
        // 先循环数字
        char[] sortNum = new char[word.length()];
        for (char aChar : chars) {
            if (aChar >= '0' && aChar <= '9') {
                int ind = aChar - '0';
                sortNum[nums[ind] - 1] = aChar;
                nums[ind] --;
            } else if (aChar >= 'a' && aChar <= 'z') {
                int ind = aChar - 'a';
                sortNum[lower[ind] + numCount + upperCount - 1] = aChar;
                lower[ind] --;
            } else if (aChar >= 'A' && aChar <= 'Z') {
                int ind = aChar - 'A';
                sortNum[upper[ind] + numCount - 1] = aChar;
                upper[ind] --;
            }
        }
        for (char c : sortNum) {
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(sort("Ihave1nose2hands10fingers"));
    }

}
